﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.BL.Common.Query;
using BEGDC.Utility.Castle;
using BEGDC.CC.BL.Common;

namespace BEGDC.CC.Web.UserControls.EV
{
    /// <summary>
    /// Summary description for SearchAPUser.
    /// </summary>
    public class SearchAPUser : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lblCustomerName;
        protected System.Web.UI.WebControls.Label lblResult;
        protected System.Web.UI.WebControls.ListBox lstboxResult;
        protected System.Web.UI.WebControls.Button btnSubmit;
        protected System.Web.UI.WebControls.ImageButton ibtnSearch;
        protected System.Web.UI.WebControls.TextBox txtName;
        protected System.Web.UI.WebControls.Button ibtnClose;

        private object DeptHeaders
        {
            get
            {
                return Session["DeptHeaders"];
            }
            set
            {
                Session["DeptHeaders"] = value;
            }
        }
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!IsPostBack)
            {
                this.Response.Expires = -1;
                SearchData(string.Empty);
                this.ibtnClose.Attributes["onclick"] = "javascript:OnClose();";
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.ibtnSearch.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnSearch_Click);
            this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion

        private void SearchData(string condition)
        {
            string type = this.Request.QueryString["Type"];
            if (type == "Owner")
            {
                IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
                DeptHeaders = queryService.SearchRoleUsersByName("APOwner", condition);
            }
            else
            {
                IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
                DeptHeaders = queryService.SearchRoleUsersByName("APContributor", condition);
            }
            lstboxResult.DataSource = DeptHeaders;
            lstboxResult.DataTextField = "NameEN";
            lstboxResult.DataValueField = "EmployeeNo";
            lstboxResult.DataBind();
        }

        private void UpdateSession()
        {
            DataTable dataTable = null;
            DataRow dataRow = null;
            object ownercontributer = Session["APOwnerContributer"];
            if (ownercontributer != null)
            {
                dataTable = (DataTable)ownercontributer;
            }
            else
            {
                return;
            }

            DataRow[] rows = ((DataTable)(this.DeptHeaders)).Select("EmployeeNo = '" + this.lstboxResult.SelectedValue + "'");
            if (rows != null && rows.Length > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    if (rows[0]["EmployeeNo"].ToString() == row["EmployeeNo"].ToString())
                    {
                        Response.Write("<script>alert('This user is already existed')</script>");
                        return;
                    }
                }

                dataRow = dataTable.NewRow();
                dataRow["NameEN"] = rows[0]["NameEN"];
                dataRow["Description"] = rows[0]["Description"];
                dataRow["APType"] = Request.QueryString["Type"].ToString();
                dataRow["EmployeeNo"] = rows[0]["EmployeeNo"];

                dataTable.Rows.Add(dataRow);
            }


            Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.returnValue='reload';window.close();</script>");

            Session["APOwnerContributer"] = this.SetRowID(dataTable);
        }

        private DataTable SetRowID(DataTable table)
        {
            if (table != null && table.Rows.Count > 0)
                for (int i = 1; i <= table.Rows.Count; i++)
                {
                    table.Rows[i - 1][0] = i.ToString();
                }
            return table;
        }

        private void ibtnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            SearchData(this.txtName.Text);
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            UpdateSession();
        }
    }
}
